package cn.anycase.demo.service.impl;

import java.util.Date;
import java.util.List;

import cn.anycase.demo.base.Response;
import cn.anycase.demo.base.SampleBeanCopyUtils;
import cn.anycase.demo.enums.SampleConstants;
import cn.anycase.demo.vo.SampleQueryInputVO;
import cn.anycase.demo.vo.SampleUpdateInputVO;
import com.baomidou.mybatisplus.plugins.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.mapper.EntityWrapper;

import cn.anycase.demo.entity.ThisIsSample;
import cn.anycase.demo.enums.GenderEnum;
import cn.anycase.demo.mapper.ThisIsSampleMapper;
import cn.anycase.demo.service.ThisIsSampleService;

/**
 * <p></p>
 * @author yuxiaobin
 * @date 2022/8/31
 */
@Slf4j
@Service
public class ThisIsSampleServiceImpl implements ThisIsSampleService {

    @Autowired
    ThisIsSampleMapper thisIsSampleMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void sampleCrud() {
        EntityWrapper<ThisIsSample> ew = new EntityWrapper<>();
        List<ThisIsSample> list = thisIsSampleMapper.selectList(ew);
        System.out.println("list.size=" + list.size());
        ThisIsSample sample = new ThisIsSample();
        sample.setGender(GenderEnum.MALE.getCode());
        sample.setAge(18);
        sample.setName("Hello World!");
        sample.setCreateTime(new Date());
        sample.setCreateBy("test");
        thisIsSampleMapper.insert(sample);
        list = thisIsSampleMapper.selectList(ew);
        System.out.println("list.size=" + list.size());
        System.out.println("id=" + sample.getId());
        ThisIsSample update = new ThisIsSample();
        update.setId(sample.getId());
        update.setGender(GenderEnum.FEMALE.getCode());
        thisIsSampleMapper.updateById(update);
        System.out.println("gender=" + thisIsSampleMapper.selectById(sample.getId()).getGender());
        throw new RuntimeException("rollback for sample");
    }

    /**
     * <p> 添加Sample记录 </p>
     * @Param   null
     * @Return  boolean
     * @Date    2022/9/21 17:20 
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Response createRecord(ThisIsSample sample) {
        try {
            sample.setCreateBy("lmh");
            sample.setCreateTime(new Date());
            Integer count = thisIsSampleMapper.insert(sample);
            if (!count.equals(SampleConstants.IS_YES)){
                return Response.success("添加成功");
            }
        } catch (Exception e) {
            log.error("系统异常");
            return Response.fail("添加失败");
        }
        return null;
    }

    /**
     * <p> 查询Sample列表 </p>
     * @param page
     * @param sampleQueryInputVO
     * @Return Page<ThisIsSample>
     * @Date 2022/9/21 19:28
     */
    @Override
    public Page<ThisIsSample> pageList(Page<ThisIsSample> page, SampleQueryInputVO sampleQueryInputVO) {
        //1,分页查询
        Page<ThisIsSample> pageInfo = new Page<>();
        List<ThisIsSample> sampleList = thisIsSampleMapper.searchList(page, sampleQueryInputVO);
        //2,设置返回结果
        pageInfo.setRecords(sampleList);
        pageInfo.setTotal(sampleList.size());
        return pageInfo;
    }

    /**
     * <p> 修改记录 </p>
     *
     * @param updateVO
     * @Param null
     * @Return Response
     * @Date 2022/9/21 22:48
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Response updateRecord(SampleUpdateInputVO updateVO) {
        try {
            Integer count = thisIsSampleMapper.updateById(SampleBeanCopyUtils.map(updateVO, ThisIsSample.class));
            if (count > 0){
                return Response.success("修改成功");
            }
        } catch (Exception e) {
            log.error("系统异常");
            return Response.fail("修改失败");
        }
        return null;
    }
}
